package cn.wolfcode.edu.service.impl;

import cn.wolfcode.edu.domain.Employee;
import cn.wolfcode.edu.domain.Role;
import cn.wolfcode.edu.mapper.EmployeeMapper;
import cn.wolfcode.edu.query.EmployeeQueryObject;
import cn.wolfcode.edu.service.IEmployeeService;
import cn.wolfcode.edu.util.PageResult;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by abb on 2018-07-08.
 */
@Service
public class EmployeeServiceImpl implements IEmployeeService{
  /*  public static void main(String[] args) {
        String enPass = new Md5Hash("aa").toString();
        System.out.println(enPass);
    }*/
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void deleteByPrimaryKey(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(Employee employee) {
        //初始密码
        String enPass = new Md5Hash("666",employee.getUsername()).toString();
        //录入时间
        employee.setInputTime(new Date());
        employee.setPassword(enPass);
        employeeMapper.insert(employee);

        //维护员工和角色的关系
        List<Role> roles = employee.getRoles();
        for(Role role:roles){
            employeeMapper.insertRelationWithRole(employee.getId() , role.getId());
        }

    }

    @Override
    public Employee selectByPrimaryKey(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> selectAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public void updateByPrimaryKey(Employee employee) {
        //密码不修改,是否是admin ,录入时间,状态
        employeeMapper.updateByPrimaryKey(employee);

        //破坏员工和角色的关系
        employeeMapper.deleteRelationWithRoleByEmployeeId(employee.getId());

        //建立员工和角色的关系
        List<Role> roles = employee.getRoles();
        for(Role role:roles){
            employeeMapper.insertRelationWithRole(employee.getId() , role.getId());
        }

    }

    public PageResult query(EmployeeQueryObject qo) {
        int total = employeeMapper.queryForCount(qo);
        List<Employee> rows = employeeMapper.queryForList(qo);
        return new PageResult(total,rows);
    }
    public List<Employee> queryForList(EmployeeQueryObject qo){
        return employeeMapper.queryForList(qo);
    }

    @Override
    public void deleteToClose(Long id) {
        employeeMapper.deleteToClose(id);
    }

    @Override
    public Employee selectByUsername(String username) {
        return employeeMapper.selectByUsername(username);
    }

    //陈遵汉 专有 勿删
    public List<Employee> selectEmployeeByHan() {
        return employeeMapper.selectEmployeeByHan();
    }

    public List<Employee> listAllEmployeeBySn(String sn) {
        return employeeMapper.listAllEmployeeBySn(sn);
    }

    @Override
    public List<Employee> listAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public Employee selectByRealName(String contents) {
        return employeeMapper.selectByRealName(contents);
    }
    //陈遵汉 专有 勿删
}
